﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Net.BLL;
using UcAsp.Net.Model;
using System.IO;
namespace UcAsp.Net.Web.Controllers
{
    public class ShowMessageController : Controller
    {
        //
        // GET: /ShowMessage/

        public ActionResult Index(int id, int? pageindex)
        {
            string view;
            ArticelsViews art;
            viewContent(id, null, Request.UserAgent, out view, out art);
            return View(view, art);
        }
        public ActionResult ShowPath(string showpath)
        {

            string view;
            ArticelsViews art;
            viewContent(null, showpath, Request.UserAgent, out view, out art);
            return View(view, art);

        }


        public ActionResult PageShow(int id, int pageindex)
        {
            string view;
            ArticlesPage page;
            viewPageContent(id, null, pageindex, Request.UserAgent, out  view, out  page);
            return View(view, page);
        }

        public ActionResult PageShowPath(string showpath, int pageindex)
        {
            string view;
            ArticlesPage page;
            viewPageContent(null, showpath, pageindex, Request.UserAgent, out  view, out  page);
            return View(view, page);
        }

        public ActionResult PageShowJson(int id, int pageindex)
        {
            ArticlesPage page = new ArticlesPageBLL().GetArticlesPageByPageID(id, pageindex);
            if (page == null)
            {
                int count = 0;
                List<ArticelsViews> list = new List<ArticelsViews>();
                ArticelsViews art = new ArticlesBLL().GetArtieById(id, out count, out list);
                page = new ArticlesPage { ArticlesID = art.ID, AddDateTime = art.AddDatetime, ArticlesContent = art.ArticleContent, CategoriesID = art.CategorieID, IconUrl = art.IconUrl, Title = art.Title, ID = 0, PageID = 1 };

            }
            return Json(page, JsonRequestBehavior.AllowGet);
        }


        private void viewContent(int? id, string path, string useragent, out string view, out ArticelsViews articles)
        {
            view = "";
            articles = new ArticelsViews();
            SettingConfig.Theme(Request.UserAgent);


            ArticelsViews art = new ArticelsViews();
            int count;
            List<ArticelsViews> list;
            if (id == null)
            {

                art = new ArticlesBLL().GetArtieByShowPath(path, out count, out list);
            }
            else
            {
               
                art = new ArticlesBLL().GetArtieById(Convert.ToInt32(id), out count, out list);
            }

            articles = art;
            ViewData["Count"] = count;
            if (art == null)
            {
                Response.Redirect("/Error/NoFound");
                return;
            }
            if (!art.IsRelease)
            {
                Response.Redirect("/Error/NoFound");
                return;
            }
            ViewData["Count"] = count;
            view = "~/themes/" + SettingConfig.Theme(Request.UserAgent) + "/ShowMessage/";
            string cateview = "~/themes/" + SettingConfig.Theme(Request.UserAgent) + "/Categories/";
            Categories categories = new CategoriesBLL().GetCategoriesById(art.CategorieID);
            if (!string.IsNullOrEmpty(art.ViewsTemp))
            {
                if (art.ViewsTemp.Trim() == "/")
                {
                    if (categories.ContentTempPath.Trim() == "/")
                    {
                        view = view + "index.cshtml";
                    }
                    else
                    {
                        view = view + categories.ContentTempPath + ".cshtml";
                    }
                }
                else
                {
                    view = view + art.ViewsTemp + ".cshtml";
                }
            }
            else
            {
                if (categories.ContentTempPath.Trim() == "/")
                {
                    view = view + "index.cshtml";
                }
                else
                {
                    view = view + categories.ContentTempPath + ".cshtml";
                }
            }

            ViewData["NextOrPrev"] = list;
            ViewData["categorieshowpath"] = categories.ShowPath;
            ViewData["Image"] = art.IconUrl;
            ViewData["Content"] = Comm.Utils.ClearPart(Comm.Utils.RemoveHtml(art.ArticleContent), 100);
            QrCodeBLL code = new QrCodeBLL();
            code.GetQrImagePath(art.ID.ToString());
            count = count + 2;
            if (count < 6)
            {
                ViewData["pagestart"] = 1;
                ViewData["pagesend"] = count;
                ViewData["EndTxt"] = "";
            }
            else
            {
                ViewData["pagestart"] = 1;
                ViewData["pagesend"] = 6;
                ViewData["EndTxt"] = "<div class='btn btn-info'>...</div>";
            }

        }

        private void viewPageContent(int? id, string path, int pageindex, string useragent, out string view, out ArticlesPage articles)
        {
            SettingConfig.Theme(Request.UserAgent);

            ArticlesPage page = new ArticlesPage();
            ArticelsViews art = new ArticelsViews();
            int count;
            List<ArticelsViews> list;
            if (id == null)
            {
                art = new ArticlesBLL().GetArtieByShowPath(path, out count, out list);
            }
            else
            {

                art = new ArticlesBLL().GetArtieById(Convert.ToInt32(id), out count, out list);
            }
            if (art == null)
            {
                Response.Redirect("/Error/NoFound");

            }
            if (!art.IsRelease)
            {
                Response.Redirect("/Error/NoFound");

            }
            ViewData["Image"] = art.IconUrl;
            ViewData["Content"] = Comm.Utils.ClearPart(Comm.Utils.RemoveHtml(art.ArticleContent), 100);
            ViewData["Count"] = count;

            page = new ArticlesPageBLL().GetArticlesPageByPageID(art.ID, pageindex);

            articles = page;
            if (page == null)
            {
                List<ArticlesPage> _list = new BLL.ArticlesPageBLL().GetArticlesPageByArticlesID(art.ID);
                ArticlesPage _page = _list.FirstOrDefault(o => o.PageID == 2);
                if (_page == null)
                {
                    foreach (ArticlesPage artpage in _list)
                    {
                        artpage.PageID = artpage.PageID - 1;
                        new BLL.ArticlesPageBLL().Update(artpage);
                    }

                }
                page = new ArticlesPageBLL().GetArticlesPageByPageID(art.ID, pageindex);
                if (page == null)
                {
                    Redirect("/Error/NoFound");
                }
            }
            view = "~/themes/" + SettingConfig.Theme(Request.UserAgent) + "/ShowMessage/";
            string cateview = "~/themes/" + SettingConfig.Theme(Request.UserAgent) + "/Categories/";
            Categories categories = new CategoriesBLL().GetCategoriesById(art.CategorieID);
            if (!string.IsNullOrEmpty(art.ViewsTemp))
            {
                if (art.ViewsTemp.Trim() == "/")
                {
                    if (categories.ContentTempPath.Trim() == "/")
                    {
                        view = view + "index_page.cshtml";
                    }
                    else
                    {
                        view = view + categories.ContentTempPath + "_page.cshtml";
                    }
                }
                else
                {

                    view = view + art.ViewsTemp + "_page.cshtml";

                }
            }
            else
            {
                if (categories.ContentTempPath.Trim() == "/")
                {
                    view = view + "index_page.cshtml";
                }
                else
                {
                    view = cateview + categories.ContentTempPath + "_page.cshtml";
                }
            }
            ViewData["categorieshowpath"] = categories.ShowPath;
            ViewData["showpath"] = String.IsNullOrEmpty(art.ShowPath) ? "" : art.ShowPath;
            ViewData["NextOrPrev"] = list;
            count = count + 2;
            if (count < 6)
            {
                ViewData["pagestart"] = 1;
                ViewData["pagesend"] = count;
                ViewData["EndTxt"] = "";
                ViewData["StartTxt"] = "";
            }
            else
            {
                int start = pageindex - 4 < 1 ? 1 : pageindex - 4;
                int end = pageindex + 4 > count ? count : pageindex +4;
                ViewData["pagestart"] = start;
                ViewData["pagesend"] = end;
                if (start > 1)
                { ViewData["StartTxt"] = "<div class='btn btn-info'>...</div>"; }
                else
                {
                    ViewData["StartTxt"] = "";
                }
                if (end < count)
                {
                    ViewData["EndTxt"] = "<div class='btn btn-info'>...</div>";
                }
                else
                { ViewData["EndTxt"] = ""; }
               
            }
        }
    }
}
